package com.tjetc.input;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

public class _1FileInputStream {
    public static void main(String[] args) {
        /*
            字节输入流：FileInputStream
            要求: 请使⽤ FileInputStream 读取 hello.txt ⽂件，并将⽂件内容显示到控制台.
         */

    }

    @Test
    public void testFileInputStream() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("/Users/gaoyongxiang/百度网盘/IDEA-Java/202507-java-se/chapter16/file/hello.txt");
        // 只能读取文件中的一个字符 按字节
//        int read = fileInputStream.read();
//        System.out.println((char) read);
//        read = fileInputStream.read();
//        System.out.println(read);
        int readData = 0;
        // 一般情况下我们都使用while来进行循环读
        // read方法当读取的值为-1时就表示读完了
        while ((readData = fileInputStream.read()) != -1) {
            System.out.println((char) readData);
        }
    }


    @Test
    public void testFileInputStream1() throws IOException {
        FileInputStream fileInputStream = null;
        try {
            // 注意：流打开是需要资源的 使用完毕后要进行关闭
            fileInputStream = new FileInputStream("/Users/gaoyongxiang/百度网盘/IDEA-Java/202507-java-se/chapter16/file/hello.txt");

            int readData = 0;
            while ((readData = fileInputStream.read()) != -1) {
                System.out.println((char) readData);
            }

        } finally {
            try {
                // 这么关 有问题
                fileInputStream.close();
            } catch (IOException e) {

            }
        }
    }


    @Test
    public void testFileInputStream2() throws Exception {
        //try- with JDK1.8 用来关闭资源的
        // 注意：要关闭的类 必须实现 AutoCloseable接口
        try (
                FileInputStream fileInputStream =
                        new FileInputStream("/Users/gaoyongxiang/百度网盘/IDEA-Java/202507-java-se/chapter16/file/hello.txt");
                A a = new A();
        ) {
            int readData = 0;
            while ((readData = fileInputStream.read()) != -1) {
                System.out.println((char) readData);
            }

        }
    }


    @Test
    public void testFileInputStream3() throws Exception {
        // 由于一个一个字节读取比较慢 我们可以使用桶
        try (
                FileInputStream fileInputStream =
                        new FileInputStream("/Users/gaoyongxiang/百度网盘/IDEA-Java/202507-java-se/chapter16/file/hello.txt");
        ) {
            byte[] bytes = new byte[64];
            int readLen = 0;
            while ((readLen = fileInputStream.read(bytes)) != -1) {
                //readLen不再是readData（数据） 表示读取的长度
//                System.out.println(readLen);
//                System.out.println(Arrays.toString(bytes));
                System.out.println(new String(bytes, 0, readLen));
            }

        }
    }

}

class A implements AutoCloseable {
    @Override
    public void close() throws Exception {

    }
}
